﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CrmControls
{
    public class MessageBx
    {
        static Hashtable m_executingPages = new Hashtable();

        public static void Show(string sMessage)
        {
            if (!m_executingPages.Contains(System.Web.HttpContext.Current.Handler))
            {
                Page executingPage = HttpContext.Current.Handler as Page;
                if ((executingPage != null))
                {
                    Queue messageQueue = new Queue();
                    messageQueue.Enqueue(sMessage);
                    m_executingPages.Add(System.Web.HttpContext.Current.Handler, messageQueue);
                    executingPage.Unload += ExecutingPage_Unload;
                }
            }
            else
            {
                Queue queue = (Queue)m_executingPages[HttpContext.Current.Handler];
                queue.Enqueue(sMessage);
            }
        }

        private static void ExecutingPage_Unload(object sender, EventArgs e)
        {
            Queue queue = (Queue)m_executingPages[HttpContext.Current.Handler];
            if ((queue != null))
            {
                StringBuilder sb = new StringBuilder();
                int iMsgCount = queue.Count;
                sb.Append("<script language='javascript'>");
                string sMsg = null;
                while (iMsgCount > 0)
                {
                    iMsgCount -= 1;
                    sMsg = (string)queue.Dequeue();
                    sMsg = sMsg.Replace("\r", "\n");
                    sMsg = sMsg.Replace("\"", "'");
                    sb.Append(("alert( \"" + sMsg + "\" );"));
                }
                sb.Append("</script>");
                m_executingPages.Remove(HttpContext.Current.Handler);
                HttpContext.Current.Response.Write(sb.ToString());
            }
        }

    }
}
